﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using CMISLibrary.Libraries;
using BaocaoNPC.Usercontrols.reports;
using BaocaoNPC.common;
using CMISLibrary;
using BCNPCEntities;
using System.Threading;

namespace CMIS2.BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for NPC_NGC_BIEU01.xaml
    /// </summary>
    public partial class NPC_NGC_BIEU01 : UserControl
    {
        /*
        private string strMa_dviqly = UserInfomation.MaDViqly != "" && UserInfomation.MaDViqly != null ? UserInfomation.MaDViqly : "PA11";
        private string strTenDNHap = UserInfomation.TenDNHap != "" && UserInfomation.TenDNHap != null ? UserInfomation.TenDNHap : "PD0400";
        private string strCurrentLibID = MenuInfomation.LibID != "" && MenuInfomation.LibID != null ? MenuInfomation.LibID : "65";
        private string strTen_dviqly = UserInfomation.TenDViQLy != "" && UserInfomation.TenDViQLy != null ? UserInfomation.TenDViQLy : "Dien luc ...";
        private string strTen_dviqly_CT = UserInfomation.TenDViQLy_CT != "" && UserInfomation.TenDViQLy_CT != null ? UserInfomation.TenDViQLy_CT : "Cong ty Dien luc ...";
         * */

        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";

        public Int16 p_thang = 0;
        public Int16 p_nam = 0;

        private string p_madviqly = "";
        private Decimal p_dientp = 0;
        private Decimal p_tiendien = 0;
        private Decimal p_CSPK = 0;
        private Decimal p_tiendien_thue = 0;
        private Decimal p_CSPK_thue = 0;


        Int16 i16ThangBC = 0;
        Int16 i16NamBC = 0;

        Int16 i16ThangInBC = 0;
        Int16 i16NamInBC = 0;
        String strMa_dviqly_InBC = "";
        String strTen_dviqly_InBC = "";
        String strTen_dviqly_CT_InBC = "";

        int intFormLoad = 0;
        public DataSet ds = new DataSet();
        DataSet ds_BCKD_In = new DataSet();
        DataSet ds_BCKD_QTTH = new DataSet();
        DataTable dt_luu = new DataTable();
        String strInBieuBCKD = "";
        //public String p_madviqly = CMISLibrary.Libraries.UserInfomation.MaDViqly.ToString();


        private void GET_THANGNAM()
        {
            try
            {
                DataTable dtThangNam = Utility.getThangNamCurrent("BC");
                if (dtThangNam != null && dtThangNam.Rows.Count > 0)
                {
                    i16ThangBC = Convert.ToInt16(dtThangNam.Rows[0]["THANG"]);
                    i16NamBC = Convert.ToInt16(dtThangNam.Rows[0]["NAM"]);
                    //i16ThangBC = 10;
                    //i16NamBC = 2009;
                }

            }
            catch
            {
                //i16ThangBC = 10;
                //i16NamBC = 2009;
                i16ThangBC = Convert.ToInt16(DateTime.Now.Month);
                i16NamBC = Convert.ToInt16(DateTime.Now.Year);
            }
        }



        public NPC_NGC_BIEU01()
        {
            InitializeComponent();            
            btnSua.IsEnabled = false;
            btnGhi.IsEnabled = false;
            btnHuy.IsEnabled = false;
            btnIn.IsEnabled = true;
            btnTonghop.IsEnabled = true;
           

            //enableTextbox(false);
            GET_THANGNAM();


            //InitializeComponent();
            if (i16ThangBC.ToString().Length == 1)
            {
                dtpThangNam.Text = "0" + i16ThangBC.ToString() + "/" + i16NamBC.ToString();
                //dtpThangNam_1.Text = "0" + i16ThangBC.ToString() + "/" + i16NamBC.ToString();
            }
            else
            {
                dtpThangNam.Text = i16ThangBC.ToString() + "/" + i16NamBC.ToString();
                //dtpThangNam_1.Text = i16ThangBC.ToString() + "/" + i16NamBC.ToString();
            }
        }

        

        private void btnSua_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dgrBIEU01.Columns[2].IsReadOnly = false;
                dgrBIEU01.Columns[4].IsReadOnly = false;
                dgrBIEU01.Columns[5].IsReadOnly = false;
                dgrBIEU01.Columns[8].IsReadOnly = false;
                dgrBIEU01.Columns[9].IsReadOnly = false;

                btnHuy.IsEnabled = true;
                btnGhi.IsEnabled = true;
                btnIn.IsEnabled = false;
                btnTonghop.IsEnabled = false;
                btnSua.IsEnabled = false;

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btnHuy_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dgrBIEU01.ItemsSource = dt_luu.DefaultView;
                btnTonghop.IsEnabled = true;
                btnSua.IsEnabled = false;
                btnGhi.IsEnabled = false;
                btnIn.IsEnabled = true;
                btnHuy.IsEnabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btnGhi_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataView dv = dgrBIEU01.ItemsSource as DataView;
                DataTable dt = dv.Table;

                    //------- ghi du lieu -------                   

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    p_madviqly = dt.Rows[i]["MA_DVIQLY"].ToString();

                    if (dt.Rows[i]["DIEN_TP"].ToString() == string.Empty)
                    {
                        p_dientp = 0;
                    }
                    else { p_dientp = Convert.ToDecimal(dt.Rows[i]["DIEN_TP"]); }

                    if (dt.Rows[i]["TIEN_DIEN"].ToString() == string.Empty)
                    {
                        p_tiendien = 0;
                    }
                    else { p_tiendien = Convert.ToDecimal(dt.Rows[i]["TIEN_DIEN"]); }

                    if (dt.Rows[i]["CSPK"].ToString() == string.Empty)
                    {
                        p_CSPK = 0;
                    }
                    else { p_CSPK = Convert.ToDecimal(dt.Rows[i]["CSPK"]); }

                    if (dt.Rows[i]["TIENTHUE_DIEN"].ToString() == string.Empty)
                    {
                        p_tiendien_thue = 0;
                    }
                    else { p_tiendien_thue = Convert.ToDecimal(dt.Rows[i]["TIENTHUE_DIEN"]); }

                    if (dt.Rows[i]["CSPKTHUE"].ToString() == string.Empty)
                    {
                        p_CSPK_thue = 0;

                    }
                    else { p_CSPK_thue = Convert.ToDecimal(dt.Rows[i]["CSPKTHUE"]); }                
             

                    GlobalServices.Service_NPCUS.NGE_UPDATE01(p_dientp, p_tiendien, p_CSPK, p_tiendien_thue, p_CSPK_thue, p_nam, p_thang, strMa_dviqly, p_madviqly);
                }
                MessageBox.Show("Ghi dữ liệu thành công");
                ds.Clear();
                ds = GlobalServices.Service_NPCUS.select_NGCBIEU01(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                ds.Tables[0].DefaultView.AllowNew = false;                
                dgrBIEU01.ItemsSource = ds.Tables[0].DefaultView;

                btnHuy.IsEnabled = false;
                btnSua.IsEnabled = true;
                btnIn.IsEnabled = true;
                btnTonghop.IsEnabled = true;
                btnGhi.IsEnabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btnIn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);

                ds_BCKD_In = GlobalServices.Service_NPCUS.select_NGCBIEU01(strTen_dviqly_InBC, strTen_dviqly_CT_InBC, strMa_dviqly, p_thang, p_nam);
                strInBieuBCKD = "BIEU 01";
                rpt_NGC_BIEU01 rpt01 = new rpt_NGC_BIEU01();
                //PC1_BIEU_11 crt = new PC1_BIEU_11();
                if (ds_BCKD_In != null)
                {
                    if (ds_BCKD_In.Tables.Count > 0)
                    {
                        ds_BCKD_In.Tables[0].TableName = "PC1_NGE_BIEU01";
                        rpt01.SetDataSource(ds_BCKD_In.Tables["PC1_NGE_BIEU01"]);
                        //UtilReport.RptManager.showReportToWin(this, rpt01, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                        UtilReport.RptManager.showReportToWin(this, rpt01, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }
                }
                else
                {
                    System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btnTonghop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                    p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                    p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);

                    if (p_thang == i16ThangBC && p_nam == i16NamBC)
                    {
                        GlobalServices.Service_NPCUS.NGE_BIEU01(strMa_dviqly, p_thang, p_nam);
                        ds = GlobalServices.Service_NPCUS.select_NGCBIEU01(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                        dt_luu = ds.Tables[0].Copy();

                        ds.Tables[0].DefaultView.AllowNew = false;
                        dgrBIEU01.ItemsSource = ds.Tables[0].DefaultView;                       
                    }
                    else
                    {
                        ds = GlobalServices.Service_NPCUS.select_NGCBIEU01(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                        dt_luu = ds.Tables[0].Copy();
                        ds.Tables[0].DefaultView.AllowNew = false;
                        dgrBIEU01.ItemsSource = ds.Tables[0].DefaultView;                
                    }
                    btnSua.IsEnabled = true;
                    btnIn.IsEnabled = true;
                    btnHuy.IsEnabled = false;
                    btnGhi.IsEnabled = false;

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            } 
        }

       

       


    }
}
